package uml.util;

import java.awt.Component;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.SwingUtilities;

public final class UMLUtility 
{
	private UMLUtility()
	{
		
	}
	public static void removeMouseListeners(Component component)
	{
		MouseListener[] m = component.getMouseListeners();
		for(MouseListener listener : m)
			component.removeMouseListener(listener);
	}
	public static Object[] getChildren(Component c)
	{
		ArrayList<Object> children = new ArrayList<Object>();
		for(int i = 0; i < SwingUtilities.getAccessibleChildrenCount(c); ++i)
		{
			Object o = SwingUtilities.getAccessibleChild(c, i);
			if(o instanceof Component)
				children.add((Component)o);
			Object[] components = getChildren((Component)o);
			for(Object object : components)
				children.add(object);
		}
		return children.toArray();
	}
	public static void removeMouseAndKeyListeners(Component c)
	{
		removeMouseListeners(c);
		removeMouseMotionListeners(c);
		removeKeyListeners(c);
	}
	public static void removeMouseMotionListeners(Component component) 
	{
		MouseMotionListener[] mouseMotionListener = component.getMouseMotionListeners();
		for(MouseMotionListener listener : mouseMotionListener)
			component.removeMouseMotionListener(listener);
	}
	public static void removeKeyListeners(Component component) 
	{
		KeyListener[] listeners = component.getKeyListeners();
		for(KeyListener listener : listeners)
			component.removeKeyListener(listener);
	}
}
